 /**
  * Copyright (C), 2015-2019, 
  * FileName: BaseService
  * Author:   陈玉翔
  * Date:     2019-06-04 9:26
  * Description: 
  * History:
  */

package com.example.workrecord.service;

 import com.example.workrecord.common.LambdaQuery;
 import com.example.workrecord.common.QueryDslTool;
 import com.example.workrecord.dao.BaseJPA;
 import com.example.workrecord.exption.CheckException;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;

 import javax.persistence.EntityManager;
 import java.io.Serializable;
 import java.math.BigDecimal;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;

/**
  *@ClassName: BaseService
  *@Description BaseService
  *@Author Mr.Bu
  *@Date 2020/4/26 11:22
  *@Version: 1.0
  */
@Service
public abstract class BaseService<T,K extends Serializable> {
     @Autowired
     protected LambdaQuery lambdaQuery;
     @Autowired
     EntityManager entityManager;
     @Autowired
     public QueryDslTool queryDslTool;

     protected abstract BaseJPA<T> getDao();
     public T findById(Long id) {
         Optional<T> byId = getDao().findById(id);
         if (!byId.isPresent()){
             return  null;
         }
         return getDao().findById(id).get();
     }

     public List<T> findAll() {
         return getDao().findAll();
     }

     @Transactional
     public T save(T entity)  {
         return  getDao().save(entity);
     }

     @Transactional
     public void saveAll(List<T> list) {
         getDao().saveAll(list);
     }

     @Transactional
     public T update(Long id,Object obj){
         Optional<T> byId = getDao().findById(id);
         if (!byId.isPresent()){
             return  null;
         }
         T one = byId.get();
         BeanUtils.copyProperties(obj,one);
         T save = getDao().save(one);
         return save;
     }

     @Transactional
     public void delete(Long id) {
         getDao().deleteById(id);
     }

     public void deleteAll(List<T> list) {
         getDao().deleteInBatch(list);
     }

     public void errorException(String msg){
         throw new CheckException(msg);
     }


     public void checkStringEmpty(String s , String errMsg){
         if (s == null || "".equals(s)){
             errorException(errMsg);
         }
     }

     public void checkObjNull(Object o , String errMsg){
         if (o == null){
             errorException(errMsg);
         }
     }

     public void checkMoenyZero(BigDecimal bigDecimal , String errMsg){
         if (bigDecimal == null || BigDecimal.valueOf(0).equals(bigDecimal)){
             errorException(errMsg);
         }
     }

     protected void errorMsg(String msg) {
         throw new CheckException(msg);
     }


     /**
      * 判断是否为空
      * @param obj
      * @return
      */
     public static boolean isEmpty(Object obj) {
         if (obj instanceof List){
             if (checkList((List)obj)){
                 return true;
             }
         }
         if (obj instanceof Map){
             if (checkMap((Map)obj)){
                 return true;
             }
         }
         if (obj == null || obj.equals("")) {
             return true;
         }

         return false;
     }
     public static void checkNull(Object obj, String msg) {
         if (obj == null) {
             throw new CheckException(msg);
         }
         if (obj instanceof List){
             if (checkList((List)obj)){
                 throw new CheckException(msg);
             }
         }
         if (obj instanceof Map){
             if (checkMap((Map)obj)){
                 throw new CheckException(msg);
             }
         }
     }
     public static Boolean checkList(List list) {
         if (list==null||list.size()<=0) {
             return  true;
         }
         return  false;
     }
     public static Boolean checkMap(Map map) {
         if (map==null||map.size()<=0) {
             return  true;
         }
         return  false;
     }

}